package org.come.Frame;

import com.tool.tcpimg.UIUtils;
import org.come.Jpanel.TestPetJpanel;
import org.come.until.FormsManagement;
import org.come.until.MessagrFlagUntil;
import org.come.until.Music;

import javax.swing.*;
import javax.swing.plaf.basic.BasicInternalFrameUI;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import java.lang.ref.WeakReference;

/**
 * @author 郑吉利
 * @date : 2017年11月22日 下午10:25:15
 *
 */

//召唤兽界面
public class TestPetJframe extends JInternalFrame implements MouseListener {

	//private TestPetJframe testPetJframe;
	private  TestPetJpanel testPetJpanel;
	private int first_x,first_y;

	// 使用WeakReference避免内存泄漏
	private WeakReference<MouseMotionListener> mouseMotionListenerRef;

	public static TestPetJframe getTestPetJframe() {
		return (TestPetJframe)FormsManagement.getInternalForm(6).getFrame();
	}


	public TestPetJframe() throws Exception {
		super();
		testPetJpanel=new TestPetJpanel();
		this.setContentPane(testPetJpanel);
		this.setBorder(BorderFactory.createEmptyBorder());//去除内部窗体的边框
		((BasicInternalFrameUI)this.getUI()).setNorthPane(null);//去除顶部的边框
		this.setBackground(UIUtils.Color_BACK);
		this.setBounds(120, 100, 100, 100);
		this.pack();
		this.setVisible(false);
		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

		// 创建鼠标移动监听器并使用WeakReference引用
		MouseMotionListener mouseMotionListener = new MouseMotionListener() {
			@Override
			public void mouseMoved(MouseEvent e) {
				if (MessagrFlagUntil.ImgFlagImg.equals(MessagrFlagUntil.MOUSE13)) {
					MessagrFlagUntil.setMouse(MessagrFlagUntil.MOUSE1);
				}
			}

			@Override
			public void mouseDragged(MouseEvent e) {
				if (isVisible()) {
					int x = e.getX() - first_x;
					int y = e.getY() - first_y;
					setBounds(x + getX(), y + getY(),getWidth(),getHeight());
				}
			}
		};

		// 保存WeakReference引用
		mouseMotionListenerRef = new WeakReference<>(mouseMotionListener);
		this.addMouseMotionListener(mouseMotionListener);
		this.addMouseListener(this);
	}

	@Override
	public void mouseClicked(MouseEvent e) {

	}

	@Override
	public void mousePressed(MouseEvent e) {
		//关闭窗体
		//开启窗口音效
		Music.addyinxiao("关闭窗口.mp3");
		//打开了窗体
		if(e.isMetaDown()){//检测鼠标右键单击
			FormsManagement.HideForm(6);
		}else {
			FormsManagement.Switchinglevel(6);
		}
		this.first_x = e.getX();
		this.first_y = e.getY();

	}

	@Override
	public void mouseReleased(MouseEvent e) {
		// TODO Auto-generated method stub

	}

	@Override
	public void mouseEntered(MouseEvent e) {
		// TODO Auto-generated method stub

	}

	@Override
	public void mouseExited(MouseEvent e) {
		// TODO Auto-generated method stub

	}

	public TestPetJpanel getTestPetJpanel() {
		return testPetJpanel;
	}


	public void setTestPetJpanel(TestPetJpanel testPetJpanel) {
		this.testPetJpanel = testPetJpanel;
	}


	public int getFirst_x() {
		return first_x;
	}

	public void setFirst_x(int first_x) {
		this.first_x = first_x;
	}

	public int getFirst_y() {
		return first_y;
	}

	public void setFirst_y(int first_y) {
		this.first_y = first_y;
	}

	/**
	 * 重写dispose方法，确保清理所有监听器引用
	 */
	@Override
	public void dispose() {
		// 清理鼠标监听器
		if (mouseMotionListenerRef != null) {
			MouseMotionListener listener = mouseMotionListenerRef.get();
			if (listener != null) {
				this.removeMouseMotionListener(listener);
			}
			mouseMotionListenerRef = null;
		}

		// 清理鼠标点击监听器
		this.removeMouseListener(this);

		// 调用父类的dispose方法
		super.dispose();
	}
}

